// gulp 的入口文件
const { src,dest,parallel, series, watch } = require('gulp');

const loadPlugin = require('gulp-load-plugins');

const plugins = loadPlugin();

const del = require('del');
const browserSync = require('browser-sync');

const bs = browserSync.create();

// 网页所需要的数据
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

const clean = () => {
  return del(['dist', 'temp']) // temp: 临时生成的目录
}

const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' }) // 第二个参数：转换的基本路径
    .pipe(plugins.sass({ outputStyle: 'expanded' })) // expanded: 将转换后的样式代码完全展开
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('dist'))
}

const serve = () => {
  watch('src/assets/styles/*.scss', style);
  watch('src/assets/scripts/*.js', script);
  watch('src/*.html', page);
  /* watch('src/assets/image/**', image);
  watch('src/assets/fonts/**', font);
  watch('public/**', extra);
 */
  watch([
    'src/assets/image/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload);

  bs.init({
    notify: false,
    port: 2000,
    // open: false,
    // files: 'dist/**',
    server: {
      baseDir: ['temp','src','public'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

const useref = () => {
  return src('temp/*.html', { base: 'dist' })
    .pipe(plugins.useref({ searchPath: ['dist', '.'] })) // 将代码中的构建注释转换
    // html js css
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({ 
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true 
    })))
    .pipe(dest('dist'))
}

const compare = parallel(style, script, page);

// 上线之前执行的任务
const build = series(
  clean, 
  parallel(
    series(compare, useref), 
    extra, 
    image, 
    font
  )
);

const develop = series(compare, serve);

module.exports = {
  clean,
  build,
  develop
}